package com.jeecms.cms.action.front;

import static com.jeecms.cms.Constants.TPLDIR_SPECIAL;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jeecms.cms.entity.bean.Domain;
import com.jeecms.cms.entity.main.CmsSite;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.FrontUtils;

@Controller
public class AjaxDomain {

	public static final String DOMAIN_INDEX = "tpl.domainindex";
		private static final Logger log = LoggerFactory
				.getLogger(AttachmentAct.class);
		
		@RequestMapping(value = "/ajaxDomain.jspx", method = RequestMethod.GET)
		public String ajaxDomainGet(
				HttpServletRequest request, HttpServletResponse response,
				ModelMap model) throws IOException {
			CmsSite site = CmsUtils.getSite(request);
			FrontUtils.frontData(request, model, site);
			return ajaxDomain(request,response,model);
		}

		@RequestMapping(value = "/ajaxDomain.jspx", method = RequestMethod.POST)
		public String ajaxDomain(
				HttpServletRequest request, HttpServletResponse response,
				ModelMap model) throws IOException {
			CmsSite site = CmsUtils.getSite(request);
			String domains = request.getParameter("domains");
			String[] suffixs = request.getParameterValues("suffix");
			String suffixList = "";
			if(suffixs==null){
				return FrontUtils.getTplPath(request, site.getSolutionPath(),
						TPLDIR_SPECIAL, DOMAIN_INDEX);
			}
			for(String suffix:suffixs){
				suffixList += suffix+",";
			}
			
			String url = suffixList.substring(0, suffixList.length()-1);
			
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost("http://www.west263.com/services/domain/whois.asp?act=query&domains="+domains+"&suffixs="+url+"&v="+Math.random());
			
			try {
				ContentProducer cp = new ContentProducer() {
					public void writeTo(OutputStream outstream) throws IOException {
						Writer writer = new OutputStreamWriter(outstream, "UTF-8");
						writer.write("");
						writer.flush();
					}
				};
				
				post.setEntity(new EntityTemplate(cp));
				HttpResponse response2 = client.execute(post);
				String temp = EntityUtils.toString(response2.getEntity());
				Domain domain = null;
				List<Domain> domainList = new ArrayList<Domain>();
				
				if(temp.startsWith("200 ok,")){
					temp = temp.substring(8);
					String a = temp.substring(2,temp.indexOf("b:")).replaceAll(";", "");
					String b = temp.substring(temp.indexOf("b:")+2,temp.indexOf("c:")).replaceAll(";", "");
					
					String[] as = a.split(",");
					for(String ass:as){
						domain = new Domain();
						String[] aaaa = ass.split("\\|");
						
						if(aaaa[0]!=null&&!"".equals(aaaa[0].trim())){
							domain.setName(aaaa[0].substring(0,aaaa[0].indexOf(".")));
							domain.setType(aaaa[0].substring(aaaa[0].indexOf(".")));
							domain.setMsg(aaaa[1]==null?"":aaaa[1]);
							domain.setPrice(aaaa[2]==null?"":aaaa[2]);
							domain.setFlag("1");
							domainList.add(domain);
						}
					}
					String[] bs = b.split(",");
					for(String bss:bs){
						domain = new Domain();
						if(bss!=null&&!"".equals(bss.trim())){
							domain.setName(bss.substring(0,bss.indexOf(".")));
							domain.setType(bss.substring(bss.indexOf(".")));
							domain.setFlag("0");
						}
						domainList.add(domain);
					}
					
					
				}else{
				}
				
				model.put("result", domainList);
				
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
			model.put("url", url);
			model.put("domains", domains);
			FrontUtils.frontData(request, model, site);
			FrontUtils.frontPageData(request, model);
			return FrontUtils.getTplPath(request, site.getSolutionPath(),
					TPLDIR_SPECIAL, DOMAIN_INDEX);
		}
}